package com.luych.userCenter.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.luych.common.page.Page;
import com.luych.userCenter.entity.PermissionInfo;
import com.luych.userCenter.entity.UserInfo;
import com.luych.userCenter.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class UserController{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final UserService userService;
    private final ObjectMapper objectMapper;
    private final PasswordEncoder passwordEncoder;

    @Autowired
    public UserController(UserService userService, ObjectMapper objectMapper, PasswordEncoder passwordEncoder) {
        this.userService = userService;
        this.objectMapper = objectMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @GetMapping("user")
    public Page page(Page page, String keywords) {
        return userService.page(page, keywords);
    }

    @GetMapping("user/{id}")
    public UserInfo getById(@PathVariable String id) {
        return userService.selectUserById(id);
    }

    @PutMapping("user")
    public UserInfo add(@RequestBody String body) throws IOException {
        UserInfo userInfo = objectMapper.readValue(body, UserInfo.class);
        if (StringUtils.isEmpty(userInfo.getUserId())) {
            userInfo.setUserId(UUID.randomUUID().toString().replace("-", ""));
        }
        if (userInfo.getPassWord() != null) {
            String passwordAfterEncode = passwordEncoder.encode(userInfo.getPassWord());
            userInfo.setPassWord(passwordAfterEncode);
        }
        return userService.insert(userInfo);
    }

    @PostMapping("user/{id}")
    public UserInfo edt(@PathVariable String id, @RequestBody String body) throws IOException {
        UserInfo userInfo = objectMapper.readValue(body, UserInfo.class);
        userInfo.setUserId(id);
        if (userInfo.getPassWord() != null) {
            String passwordAfterEncode = passwordEncoder.encode(userInfo.getPassWord());
            userInfo.setPassWord(passwordAfterEncode);
        }
        return userService.update(userInfo);
    }

    @DeleteMapping("user/{id}")
    public void del(@PathVariable String id) {
        userService.delete(id);
    }

    @GetMapping("login/{loginName}/{passWord}")
    public UserInfo login(@PathVariable String loginName, @PathVariable String passWord) {
        return userService.selectByUserNameAndPassWord(loginName, passWord);
    }

    @GetMapping("user/by/loginName/{loginName}")
    public UserInfo getByLoginName(@PathVariable String loginName) {
        return userService.selectUserByLoginName(loginName);
    }

    @GetMapping("permissions/by/user/{userId}")
    public List<PermissionInfo> permissionsByUser(@PathVariable String userId) {
        return userService.permissionsByUser(userId);
    }
}
